//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js')
Page({
  data: {    
    date: '',
    time_go:'',
    start_date:'',
    end_date: '',
    begin: '北京',
    end: '上海',
    high_speed:false,
    student:false,
    departure_time:'',
    items: [
      { name: 'student', value: '学生票' },
      //checked为truetrue，所以该项默认选中
      { name: 'speed', value: '高铁动车' },
    ]
  }, 

  onLoad: function (options) { 
    var that = this
    wx.request({
      url: "https://wx.dong.sowtai.com/test/time.php", 

      success: function (res) {
        //console.log(res)
        if (res.statusCode == 200) {
          var time = res.data.today
          var times = time.split("-")
          var dates = times[1] + "月" + times[2] + "日"
          that.setData({
            departure_time: time,
            date: dates,
            start_date:res.data.today,
            end_date: res.data.later_time
          })
        }
      }
    })
  },


  onReady: function () {
    var that = this

  
  },
  //日期选择器
  bindDateChange (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    var time = e.detail.value
    var times = time.split("-")
    var dates = times[1] + "月" + times[2] + "日"
    this.setData({
      date: dates,
      time_go: time
    })
  },
   //城市选择器
  bindMultiPickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindBeginCityView: function () {
    wx.navigateTo({
      url: '../city/city?cityType=begin',
    })
  },
  bindEndCityView: function () {
    wx.navigateTo({
      url: '../city/city?cityType=end',
    })
  },
  bindSubmit: function () {
    var that = this;
    if ( that.data.time_go == "" ) {
      var departure_time = that.data.departure_time
      app.globalData.departure_time = departure_time;
      console.log(app.globalData);
    }
    if (that.data.time_go !== "") {
      var departure_time = that.data.time_go
    }
    // console.log(app.globalData.trainBeginCity + app.globalData.trainEndCity);
    wx.navigateTo({
      url: '../result/result',
    })   
  },
  checkboxChange: function (e) {

    if (e.detail.value == "student"){
      this.setData({
        student:true,
        high_speed: false
      })
    }
    if (e.detail.value == "speed") {
      this.setData({
        student: false,
        high_speed: true
      })
    }
    if (e.detail.value.length == 2) {
      this.setData({
        student: true,
        high_speed: true
      })
    }
    if (e.detail.value.length == "") {
      this.setData({
        student: false,
        high_speed: false
      })
    }
    var that = this
    //console.log("高铁:" + that.data.high_speed + "----" + "学生票" + that.data.student)


  },
  onShow: function () {
    this.setData({ begin: app.globalData.trainBeginCity })
    this.setData({ end: app.globalData.trainEndCity })
  }
})

